package com.qf.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author 汤奇
 * @version V1.0
 * @Project BookPro
 * @Package com.qf.config
 * @Description:    初始化restTemplate对象
 * @Date 2022/7/6 16:34
 */
@Configuration
public class RestTemplateConfig {

    /**
     * 初始化restTemplate对象
     * @param factory
     * @return
     */
    @Bean
    //给远程调用对象中加入负载均衡技术,默认是轮询
    @LoadBalanced
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory (){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000); //ms
        factory.setConnectTimeout(15000); //ms
        return factory;
    }


}
